package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRuleDistrict;
import com.pubinfo.passbook.common.entity.TRuleRole;
import com.pubinfo.passbook.common.entity.TSystemRole;
import com.pubinfo.passbook.common.mapper.TRuleDistrictMapper;
import com.pubinfo.passbook.common.mapper.TRuleRoleMapper;
import com.pubinfo.passbook.common.model.dto.rule.RuleRole;
import com.pubinfo.passbook.common.model.dto.rule.RuleRoleParam;
import com.pubinfo.passbook.common.model.http.eip.UserInfo;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.rule.RuleRoleIdRelevanceVo;
import com.pubinfo.passbook.common.service.master.TRuleRoleService;
import com.pubinfo.passbook.common.service.ruler.CheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.pubinfo.passbook.common.model.constant.RoleEnum.ADMIN;

/**
 *
 */
@Service
@Slf4j
public class TRuleRoleServiceImpl extends ServiceImpl<TRuleRoleMapper, TRuleRole>
    implements TRuleRoleService {
  
  @Resource
  private TRuleDistrictMapper districtMapper;
  @Resource
  private CheckService userInfoService;
  
  @Override
  public boolean saveRuleRole(RuleRole role, UserInfo userInfo, TSystemRole systemRole) {
//    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
//      if (!systemRole.getDistrictId().equals(role.getDistrictId()))
//        return false;
//    }
    TRuleDistrict tRuleDistrict = districtMapper.selectById(role.getDistrictId());
    if (tRuleDistrict != null)
      return this.baseMapper.insert(role) > 0;
    return false;
  }
  
  @Override
  public IPage<TRuleRole> selectRuleRole(RuleRoleParam param, UserInfo userInfo, TSystemRole systemRole) {
    IPage<TRuleRole> iPage = page(param.getPage());
//    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
//      param.setDistrictId(systemRole.getDistrictId());
//      return this.baseMapper.selectRuleRole(iPage, param);
//    }
    if (userInfoService.judgeRole(ADMIN.getRoleCode(), userInfo))
      return this.baseMapper.selectRuleRole(iPage, param);
    return new Page();
  }
  
  @Override
  public JsonResult<String> deleteRuleRole(String id, UserInfo userInfo, TSystemRole systemRole) {
//    TRuleRole role = this.baseMapper.selectById(id);
//    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
//      if (!systemRole.getDistrictId().equals(role.getDistrictId()))
//        return JsonResult.error("只能删除所属区县的规则角色");
//    }
    List<RuleRoleIdRelevanceVo> ruleRoleIdRelevanceVos = this.baseMapper.selectRoleIdRelevance(id);
    ruleRoleIdRelevanceVos.removeIf(Objects::isNull);
    if (!ruleRoleIdRelevanceVos.isEmpty())
      return JsonResult.error("该数据被其他表引用，删除失败");
    this.baseMapper.deleteRoleReimbursable(id);
    this.baseMapper.deleteById(id);
    return JsonResult.success("删除成功");
  }
  
  @Override
  public JsonResult<String> updateRuleRole(RuleRole ruleRole, UserInfo userInfo, TSystemRole systemRole) {
//    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
//      if (!systemRole.getDistrictId().equals(ruleRole.getDistrictId()))
//        return JsonResult.error("只能修改所属区县的规则角色");
//    }
    TRuleDistrict tRuleDistrict = districtMapper.selectById(ruleRole.getDistrictId());
    if (tRuleDistrict == null)
      return JsonResult.error("没有相关的district数据,更新失败");
    this.baseMapper.updateById(ruleRole);
    return JsonResult.success("更新成功");
  }
}




